package com.cqc.zhbjcqc.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.cqc.zhbjcqc.R;
import com.cqc.zhbjcqc.adapter.GuildPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuildActivity extends AppCompatActivity {
    private Context context;
    private ViewPager viewPager;
    private Button btn;
    private List<ImageView> list =  new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guild);
        context = this;

        findViews();
        initViews();
    }

    private void initViews() {
        initViewPager();
        initButton();
    }

    private void initButton() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initViewPager() {
        initImages();
        GuildPagerAdapter adapter = new GuildPagerAdapter(list);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == list.size()-1){
                    btn.setVisibility(View.VISIBLE);
                } else {
                    btn.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initImages() {
        list.clear();
        ImageView iv1 = new ImageView(context);
        ImageView iv2 = new ImageView(context);
        ImageView iv3 = new ImageView(context);

        iv1.setBackgroundResource(R.mipmap.guide_1);
        iv2.setBackgroundResource(R.mipmap.guide_2);
        iv3.setBackgroundResource(R.mipmap.guide_3);

        list.add(iv1);
        list.add(iv2);
        list.add(iv3);
    }

    private void findViews() {
        viewPager = (ViewPager) findViewById(R.id.viewPager_guild);
        btn = (Button) findViewById(R.id.btn_guild);
    }
}
